package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.workflow.dao.BpmDefVarDao;
import com.ccp.dev.workflow.model.BpmDefVar;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe:流程变量定义 Service
 * @author :jiayueqiao
 * Date: 2019-05-15
 * Since:1
 */
@Service
public class BpmDefVarService extends BaseService<BpmDefVar> {


    @Resource
    private BpmDefVarDao dao;

    /**
     * 判断变量名是否存在
     *
     * @param varName 变量名称
     * @param varKey  变量key
     * @param modelId 模型定义id
     * @return 变量名是否存在
     */
    public boolean isVarNameOrVarKeyExist(String varName, String varKey, String modelId) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("varName", varName);
        params.put("varKey", varKey);
        params.put("modelId", modelId);
        int count = dao.isVarNameOrVarKeyExist(params);
        return count > 0;
    }


    /**
     * 根据模型定义id获取变量列表
     *
     * @param modelId 模型定义id
     * @return 变量列表
     */
    public List<BpmDefVar> queryByModelId(String modelId) {
        return dao.queryByModelId(modelId);
    }

    /**
     * 根据主键集合批量删除
     *
     * @param ids 数据主键数组
     */
    @Transactional(rollbackFor = Exception.class)
    public void delVarsByIdArray(String[] ids) {
        for (String id : ids) {
            dao.delById(id);
        }
    }
}
